//
//  NSString+Encoding.m
//  SWIOSLibary
//
//  Created by shaowei on 4/17/14.
//  Copyright (c) 2014 LianZhan. All rights reserved.
//

#import "NSString+Encoding.h"

@implementation NSString (Encoding)

+ (NSString *)stringFromUnicode:(NSString *)unicodeStr {
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];
    
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}

+ (NSString *)stringFromGBK:(NSString *)gbkStr{
    NSURL *url = [NSURL URLWithString:gbkStr];
    NSData *responseData = [NSData dataWithContentsOfURL:url];
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:enc];
    return responseString;
}

@end
